<?php

require_once('../init.php');

defined("IN_CA") or die();

require_once(CA_ROOT . 'config.php');
require_once(CA_KERNEL . 'MySqlWrapper.class.php');
require_once(CA_KERNEL . 'TemplateHandler.class.php');
require_once(CA_KERNEL . 'Controller.class.php');
require_once(CA_KERNEL . 'Validate.class.php');
require_once(CA_KERNEL . 'Cookie.class.php');
require_once(CA_KERNEL . 'User.php');
require_once(CA_KERNEL . 'Password.class.php');

$sql = new MySqlWrapper($config['hostname'], $config['username'], $config['password'], $config['database'], false);

define("ADM_TEMPLATE_DIR", CA_ADMIN."templates/");

class AdminIndexController extends Controller
{
	/**
	 * MySqlWrapper object
	 *
	 * @var MySqlWrapper
	 */
	protected $db;
	
	/**
	 * TemplateHandler object
	 *
	 * @var TemplateHandler
	 */
	protected $template;
	
	/**
	 * User object ( If set )
	 *
	 * @var User
	 */
	protected $user;
	
	/**
	 * Constructor
	 * 
	 * Not much in here..
	 */
	public function __construct()
	{
		$this->db = &$GLOBALS['sql'];
	}
	
	protected function checkSession()
	{
		$data = explode(">>>", urldecode(Cookie::get("adm_session")));
		
		$this->user = new User($this->db);
		$login = $this->user->login($data[0], $data[1], false);
		
		if($login != UserLookupCode::SUCCESS)
		{
			return false;
		}
		
		$this->updateSession();
		return true;
	}
	
	protected function updateSession()
	{
		Cookie::set("adm_session", Cookie::get("adm_session"), 3600);
	}
	
	protected function actionIndex()
	{
		if(!Cookie::exists("adm_session") | !$this->checkSession())
		{
			$this->actionLogin();
			return;
		}
		
		$this->template = new TemplateHandler("adm_index.php", ADM_TEMPLATE_DIR);
		$this->template->assign('user', $this->user);
		
		$this->template->display();
	}
	
	protected function actionLogin()
	{
		if(Cookie::exists("adm_session") && $this->checkSession())
		{
			header("Location: ?action=Index");
		}
		
		$this->template = new TemplateHandler("adm_login.php", ADM_TEMPLATE_DIR);
		
		if(strtolower($_SERVER['REQUEST_METHOD']) == "post")
		{
			$username = $_POST['username'];
			$password = $_POST['password'];
			
			$criteria = array(
				"length"		=> array("min" => 4, "max" => 32),
				"regex_match"	=> "#^[-a-z0-9_.]+$#si"
			);
			
			$errors = array();
			
			if(!Validate::string($username, $criteria))
			{
				$errors[] = "Brugernavn er ugyldigt.";
			}
			
			if(!Validate::string($password, $criteria))
			{
				$errors[] = "Kodeord er ugyldigt.";
			}
			
			if(count($errors) == 0)
			{
				$user = new User($this->db);
				
				$logincode = $user->login($username, $password);
				
				if($logincode != UserLookupCode::SUCCESS)
				{
					switch ($logincode)
					{
						case UserLookupCode::BANNED:
							$errors[] = "Brugeren er banlyst.";
							break;
						case UserLookupCode::NOT_FOUND:
							$errors[] = "Brugeren findes ikke.";
							break;
						case UserLookupCode::PENDING:
							$errors[] = "Brugeren er endnu ikke aktiveret.";
							break;
						case UserLookupCode::SQL_ERROR:
							$errors[] = "MySQL-fejl: [" . mysql_errno() . "] " . mysql_error();
							break;
					}
				}
				else
				{
					Cookie::set("adm_session", $username . ">>>" . Password::encrypt($username, $password), 3600);
					header("Location: ?action=Index");
				}
			}
			
			$this->template->assign('errors', $errors);
		}
		
		$this->template->display();
	}
	
	protected function actionLogout()
	{
		if(Cookie::exists("adm_session") && $this->checkSession())
		{
			Cookie::set("adm_session", 0, 0);
			header("Location: ?");
			return;
		}
	}
	
	protected function actionModules()
	{
		if(!Cookie::exists("adm_session") | !$this->checkSession())
		{
			$this->actionLogin();
			return;
		}
		
		if(!isset($_GET['module']))
		{
			$this->template = new TemplateHandler("adm_modules.php", ADM_TEMPLATE_DIR);
		}
		else
		{
			$this->dispatchModule($_GET['module']);
		}
		
		$this->template->assign('user', $this->user);
		
		$this->template->display();
	}
	
	protected function dispatchModule($module)
	{
		if(file_exists(CA_ADMIN."modules/".$module.".php"))
		{
			include CA_ADMIN."modules/".$module.".php";
			return true;
		}
		else
		{
			$this->template = new TemplateHandler("404.html", ADM_TEMPLATE_DIR);
			return false;
		}
	}
	
	protected function actionHelp()
	{
		if(!Cookie::exists("adm_session") | !$this->checkSession())
		{
			$this->actionLogin();
			return;
		}
		
		$this->template = new TemplateHandler("adm_index.php", ADM_TEMPLATE_DIR);
		$this->template->assign('user', $this->user);
		
		$this->template->display();
	}
	
	protected function actionStatistics()
	{
		if(!Cookie::exists("adm_session") | !$this->checkSession())
		{
			$this->actionLogin();
			return;
		}
		
		$this->template = new TemplateHandler("adm_index.php", ADM_TEMPLATE_DIR);
		$this->template->assign('user', $this->user);
		
		$this->template->display();
	}
	
	protected function actionSettings()
	{
		if(!Cookie::exists("adm_session") | !$this->checkSession())
		{
			$this->actionLogin();
			return;
		}
		
		$this->template = new TemplateHandler("adm_index.php", ADM_TEMPLATE_DIR);
		$this->template->assign('user', $this->user);
		
		$this->template->display();
	}
}

$aic = new AdminIndexController();
@$aic->dispatch($_GET['action']);

?>